#include "osservice.h"
#include <QNetworkInterface>
#include "commandexecutor.h"
#include <QEventLoop>

OsService::OsService(QObject *parent) : QObject(parent)
{

}

QHostAddress OsService::getHostAddress()
{
    foreach (QNetworkInterface ni, QNetworkInterface::allInterfaces())
    {
        qDebug() << ni;
        // en***0
        if (ni.name().startsWith("en") && ni.IsUp && ni.IsRunning)
        {
            QList<QNetworkAddressEntry> list = ni.addressEntries();
            QNetworkAddressEntry entry = list.at(0);
            return entry.ip();
        }
    }
}

QHostAddress OsService::getBroadCastAddress()
{
    foreach (QNetworkInterface ni, QNetworkInterface::allInterfaces())
    {
        qDebug() << ni;
        // en***0
        if (ni.name().startsWith("en") && ni.IsUp && ni.IsRunning)
        {
            QList<QNetworkAddressEntry> list = ni.addressEntries();
            QNetworkAddressEntry entry = list.at(0);
            return entry.broadcast();
        }
    }
}

void OsService::test()
{
    QString cmd = "/bin/bash -c \"tty\"";
    CommandExecutor ce(cmd);
    // 添加消息循环机制...
    QEventLoop eventloop;
    connect(&ce, SIGNAL(finishedEvent()), &eventloop, SLOT(quit()));
    ce.start();
    qDebug() << "eventLoop executing";
    eventloop.exec();
    qDebug() << "eventLoop executed";
    int exitCode = ce.getExitCode();
    qDebug() << exitCode;
}
